package com.lu.manage.core.common.node;

import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * layui tableTree所需的数据结构
 */
public class LayuiTreeTable {

    //id
    private Long id;
    //父id
    private Long pid;
    //文件目录
    private String fileDir;
    //文件名称
    private String fileName;
    //文件大小
    private String fileSize;
    //文件创建时间
    private String createTime;

    /**
     * 顶级 -> 若干父节点 -> 每个父节点对应许多子节点
     * @param rootPath
     * @return
     */
    public static List<LayuiTreeTable> fileList(String rootPath){
        List<LayuiTreeTable> layuiTreeTableList = new ArrayList<>();
        LayuiTreeTable layuiTreeTable = null;
        File file = new File(rootPath);
        //取出父目录文件夹
        String[] dirList = file.list();
        if(dirList != null && dirList.length > 0){
            long index = 1;
            long zindex = index;
            //迭代父目录文件夹
            for(String dir : dirList){
                //查找每一个父文件夹下的文件
                file = new File(rootPath + File.separator +dir);
                File[] fs = file.listFiles();
                if(fs != null && fs.length > 0){
                    long dirSize = 0;
                    LayuiTreeTable treeTable = new LayuiTreeTable();
                    treeTable.setId(index);
                    treeTable.setPid(0L);
                    treeTable.setFileDir(dir);
                    treeTable.setFileName(dir);
                    zindex ++;
                    for(File f : fs){
                        long fileSize = div(f.length()).longValue();
                        layuiTreeTable = new LayuiTreeTable();
                        layuiTreeTable.setPid(index);
                        layuiTreeTable.setId(zindex);
                        layuiTreeTable.setFileDir(dir);
                        layuiTreeTable.setFileName(f.getName());
                        layuiTreeTable.setFileSize(fileSize + "kb");
                        layuiTreeTable.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(f.lastModified()));
                        layuiTreeTableList.add(layuiTreeTable);
                        dirSize += fileSize;
                        zindex ++;
                    }
                    treeTable.setFileSize(dirSize + "kb");
                    treeTable.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                    layuiTreeTableList.add(treeTable);
                    index = zindex;
                }
            }
        }
        return layuiTreeTableList;
    }

    public static void main(String[] args) {
        List<LayuiTreeTable> ls = LayuiTreeTable.fileList("/Users/zhanglu/Desktop/lu/jingluu_workspace/project_file");
        System.out.println("111");
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getPid() {
        return pid;
    }

    public void setPid(Long pid) {
        this.pid = pid;
    }

    public String getFileDir() {
        return fileDir;
    }

    public void setFileDir(String fileDir) {
        this.fileDir = fileDir;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileSize() {
        return fileSize;
    }

    public void setFileSize(String fileSize) {
        this.fileSize = fileSize;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    /**
     * 除法
     * @param num
     * @return
     */
    public static BigDecimal div(Long num){
        BigDecimal b1 = new BigDecimal(num);
        BigDecimal b2 = new BigDecimal(1024);
        return b1.divide(b2,0,BigDecimal.ROUND_HALF_UP);
    }

}
